import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_plugin_print/flutter_plugin_print.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await FlutterPluginPrint.platformVersion ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              ElevatedButton(
                child: Text("打印小票Demo"),
                onPressed: () async {
                  await FlutterPluginPrint.demo();
                },
              ),
              SizedBox(height: 30),
              ElevatedButton(
                child: Text("打印文本"),
                onPressed: () async {
                  await FlutterPluginPrint.printText("测试文本！测试文本！！！");
                },
              ),
              SizedBox(height: 30),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    child: Text("切刀-半切"),
                    onPressed: () async {
                      await FlutterPluginPrint.halfCut();
                    },
                  ),
                  SizedBox(width: 30),
                  ElevatedButton(
                    child: Text("切刀-全切"),
                    onPressed: () async {
                      await FlutterPluginPrint.allCut();
                    },
                  ),
                ],
              ),
              SizedBox(height: 30),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    child: Text("1倍宽1倍高"),
                    onPressed: () async {
                      await FlutterPluginPrint.setFont(0, 1, 1, 0, 0);
                      await FlutterPluginPrint.printText('1倍宽1倍高');
                    },
                  ),
                  SizedBox(width: 30),
                  ElevatedButton(
                    child: Text("2倍宽2倍高"),
                    onPressed: () async {
                      await FlutterPluginPrint.setFont(0, 2, 2, 0, 0);
                      await FlutterPluginPrint.printText('2倍宽2倍高');
                    },
                  ),
                  SizedBox(width: 30),
                  ElevatedButton(
                    child: Text("粗体"),
                    onPressed: () async {
                      await FlutterPluginPrint.setFont(0, 0, 0, 1, 0);
                      await FlutterPluginPrint.printText('粗体下划线');
                    },
                  ),
                ],
              ),
              SizedBox(height: 30),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    child: Text("居左文字"),
                    onPressed: () async {
                      await FlutterPluginPrint.alignLeft();
                      await FlutterPluginPrint.printText('居左文字');
                    },
                  ),
                  SizedBox(width: 30),
                  ElevatedButton(
                    child: Text("居右文字"),
                    onPressed: () async {
                      await FlutterPluginPrint.alignRight();
                      await FlutterPluginPrint.printText('居右文字');
                    },
                  ),
                  SizedBox(width: 30),
                  ElevatedButton(
                    child: Text("居中文字"),
                    onPressed: () async {
                      await FlutterPluginPrint.alignCenter();
                      await FlutterPluginPrint.printText('居中文字');
                    },
                  ),
                  SizedBox(width: 30),
                  ElevatedButton(
                    child: Text("空白2行"),
                    onPressed: () async {
                      await FlutterPluginPrint.newLine(2);
                      await FlutterPluginPrint.printText('空白2行');
                    },
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
